package com.zhongjian.technologycal.mall.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.githang.statusbar.StatusBarCompat;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.fragment.HomeFragment;
import com.zhongjian.technologycal.mall.fragment.PersonalCenterFragment;
import com.zhongjian.technologycal.mall.fragment.ShopCategoryFragment;
import com.zhongjian.technologycal.mall.fragment.ShoppingCartFragment;
import com.zhongjian.technologycal.mall.fragment.TodayFragment;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页面，主要实现tab页面、
 * 1）首页
 * 2）分类
 * 3）今日众建
 * 4）购物车
 * 5）我
 */

public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {
    private BottomNavigationBar bottomNavigationBar;
    //    private BadgeItem badgeItem;
    private TextBadgeItem badgeItem;
    int lastSelectedPosition = 0;
    // 保存用户按返回键的时间
    private long mExitTime = 0;
    private String TAG = MainActivity.class.getSimpleName();
    private String titleTab [] = {"首页","分类","健康圈","购物车","我的"};
    private List<Fragment> fragmentList = null;
    private Fragment preFragment;


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initViews();
        setDefaultFragment();
        StatusBarCompat.resetActionBarContainerTopMargin(getWindow(), android.support.v7.appcompat.R.id.action_bar_container);
        bottomNavigationBar = findViewById(R.id.bottom_navigation_btn);
        bottomNavigationBar.setTabSelectedListener(this);
        badgeItem = new TextBadgeItem();
//        badgeItem1 = new BadgeItem();
//        badgeItem.setHideOnSelect(false)
//                .setBackgroundColorResource(R.color.color_items_red)
//                .setAnimationDuration(5000)
//                .setText("1")
//                .setBorderWidth(1);
        /**
         * 包含3种Mode：
         * MODE_DEFAULT如果Item的个数<=3就会使用MODE_FIXED模式，否则使用MODE_SHIFTING模式
         * MODE_FIXED （固定大小）填充模式，未选中的Item会显示文字，没有换挡动画。
         * MODE_SHIFTING （不固定大小）换挡模式，未选中的Item不会显示文字，选中的会显示文字。在切换的时候会有一个像换挡的动画
         */

        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);

        bottomNavigationBar.addItem(new BottomNavigationItem(
                R.mipmap.home_blue2x, titleTab[0])
                .setActiveColorResource(R.color.blue)
                .setInActiveColor(R.color.colorTab)
                .setInactiveIcon(ContextCompat.getDrawable(getApplicationContext(),
                        R.mipmap.home_gray2x)))
                .addItem(new BottomNavigationItem(
                        R.mipmap.classification_blue2x, titleTab[1])
                        .setActiveColorResource(R.color.blue)
                        .setInActiveColor(R.color.colorTab)
                        .setInactiveIcon(ContextCompat.getDrawable(getApplicationContext(),
                                R.mipmap.classification2x)))
                .addItem(new BottomNavigationItem(
                        R.mipmap.health_blue2x, titleTab[2])
                        .setActiveColorResource(R.color.blue)
                        .setInActiveColor(R.color.colorTab)
                        .setInactiveIcon(ContextCompat.getDrawable(getApplicationContext(),
                                R.mipmap.healthy2x)))
                .addItem(new BottomNavigationItem(
                        R.mipmap.shoppingcart_blue2x, titleTab[3])
                        .setActiveColorResource(R.color.blue)
                        .setInActiveColor(R.color.colorTab)
                        .setInactiveIcon(ContextCompat.getDrawable(getApplicationContext(),
                                R.mipmap.shoppingcart_gray2x)))
                .addItem(new BottomNavigationItem(
                        R.mipmap.my_blue2x, titleTab[4])
                        .setActiveColorResource(R.color.blue)
                        .setInActiveColor(R.color.colorTab)
                        .setInactiveIcon(ContextCompat.getDrawable(getApplicationContext(),
                                R.mipmap.my_gray2x)))//依次添加item,分别icon和名称
                .setFirstSelectedPosition(0)//设置默认选择item
                .initialise();//初始化


//        bottomNavigationBar.addItem(new BottomNavigationItem(
//                R.mipmap.years013x, titleTab[0])
//                .setActiveColorResource(R.color.color_items_red)
//                .setInActiveColor(R.color.colorTab)
//                .setInactiveIcon(ContextCompat.getDrawable(getApplicationContext(),
//                        R.mipmap.years013x)))
//                .addItem(new BottomNavigationItem(
//                        R.mipmap.years0022x, titleTab[1])
//                        .setActiveColorResource(R.color.color_items_red)
//                        .setInActiveColor(R.color.colorTab)
//                        .setInactiveIcon(ContextCompat.getDrawable(getApplicationContext(),
//                                R.mipmap.years0022x)))
//                .addItem(new BottomNavigationItem(
//                        R.mipmap.years0052x, titleTab[2])
//                        .setActiveColorResource(R.color.color_items_red)
//                        .setInActiveColor(R.color.colorTab)
//                        .setInactiveIcon(ContextCompat.getDrawable(getApplicationContext(),
//                                R.mipmap.years0052x)))
//                .addItem(new BottomNavigationItem(
//                        R.mipmap.years0032x, titleTab[3])
//                        .setActiveColorResource(R.color.color_items_red)
//                        .setInActiveColor(R.color.colorTab)
//                        .setInactiveIcon(ContextCompat.getDrawable(getApplicationContext(),
//                                R.mipmap.years0032x)))
//                .addItem(new BottomNavigationItem(
//                        R.mipmap.years0042x, titleTab[4])
//                        .setActiveColorResource(R.color.color_items_red)
//                        .setInActiveColor(R.color.colorTab)
//                        .setInactiveIcon(ContextCompat.getDrawable(getApplicationContext(),
//                                R.mipmap.years0042x)))//依次添加item,分别icon和名称
//                .setFirstSelectedPosition(0)//设置默认选择item
//                .initialise();//初始化
    }

    private void initViews(){
        fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new ShopCategoryFragment());
        fragmentList.add(new TodayFragment());
        fragmentList.add(new ShoppingCartFragment());
        fragmentList.add(new PersonalCenterFragment());

    }
    @Override
    public void initPresenter() {

    }
    /**
     * 设置默认的Fragment
     */

    private void setDefaultFragment() {
        changeFragment(0);
    }


    /**
     * 切换fragment
     * @param index
     */
    public void changeFragment(int index){
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        Fragment fragment = fragmentList.get(index);
        if (preFragment != null) {
            transaction.hide(preFragment);
        }
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(R.id.fragment_content, fragment);
        }
        transaction.commit();
        preFragment = fragment;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return super.isSupportSwipeBack();
    }

    /**
     *
     * @param position
     * 未选中 -> 选中
     *
     */
    @Override
    public void onTabSelected(int position) {
        switch (position) {
            case 0:
                /**
                 * 首页
                 */
                changeFragment(0);
                StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.border_color), true);
                break;
            case 1:
                /**
                 * 分类
                 */
                changeFragment(1);
                StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.border_color), true);
                break;
            /**
             * 健康圈
             */
            case 2:
                changeFragment(2);
                StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.border_color), true);
                break;
            /**
             * 购物车
             */
            case 3:
                changeFragment(3);
                StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.border_color), true);
                break;
            /**
             * 我的（个人中心）
             */
            case 4:
                changeFragment(4);
                StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.blue), true);
                break;
            default:
                break;

        }


    }

    /**
     *
     * @param position
     * 选中 -> 未选中
     */
    @Override
    public void onTabUnselected(int position) {
    }

    /**
     *
     * @param position
     * 选中 -> 选中
     */
    @Override
    public void onTabReselected(int position) {
//        Log.d("onTabReselected", position + "");
    }


    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(this, "再按一次退出程序哦", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        HomeFragment.ishand = true;
        return super.onKeyDown(keyCode, event);
    }
}
